// file name: example.kt
package foo
private fun foo() { ... } // visible inside example.kt
public var bar: Int = 5 // property is visible everywhere
private set // setter is visible only in example.kt
internal val baz = 6 // visible inside the same module
|
fileprivate func foo() { ... } // visible inside example.swift
public private (set) var bar: Int = 5 // property is visible everywhere
// setter is visible only in A
internal let baz = 6 // visible inside the same module
|
open class Outer {
private val a = 1
protected open val b = 2
internal val c = 3
val d = 4 // public by default
protected class Nested {
public val e: Int = 5
}
}
class Subclass : Outer() {
// a is not visible
// b, c and d are visible
// Nested and e are visible
override val b = 5 // 'b' is protected
}
class Unrelated(o: Outer) {
// o.a, o.b are not visible
// o.c and o.d are visible (same module)
// Outer.Nested is not visible, and Nested::e is not visible either
}
|
/*
open and public
Open access and public access enable entities to be used within any source file from their defining module
*/
open class A { //open for class, allowing code outside the module to subclass and override
open var a: Int = 0
}
public struct B {
public var b: Int = 0
}
/*
internal, default
Internal access enables entities to be used within any source file from their defining module
*/
internal let a = ""
let someInternalConstant = 0 // implicitly internal
class C { } // implicitly internal
/*
fileprivate
File-private access restricts the use of an entity to its own defining source file
*/
fileprivate let b = ""
/*
private
Private access restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file.
*/
class D {
private let d = 0
}
extension D {
func foo() {
print(d)
}
}
👏
|